home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 June: Reference Library / Dev.CD Jun 99 RL Disk 1.toast / Technical Documentation / Develop / develop Issue 28 / develop Issue 28 code / Merge Tools / analyze.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-06  |  27.8 KB  |  919 lines  |  [TEXT/MPS ]

  1. /* Analyze file differences for Eclectus integration utilities.
  2.      Copyright (C) 1992-96 Eclectus (D. John Anderson, Alan B. Harper).
  3.  
  4. This file is part of the Eclectus integration utilities.
  5.  
  6. Eclectus integration utilities are free software; you can redistribute
  7. it and/or modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 1, or
  9. (at your option) any later version.
  10.  
  11. Eclectus integration utilities is distributed in the hope that it
  12. will be useful, but WITHOUT ANY WARRANTY; without even the implied
  13. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. See the GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with the Eclectus integration utilities; see the file COPYING.
  18. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
  19. MA 02139, USA.    */
  20.  
  21. /* The basic algorithm is described in:
  22.    "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
  23.    Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
  24.    see especially section 4.2, which describes the variation used below.
  25.    Unless the --minimal option is specified, this code uses the TOO_EXPENSIVE
  26.    heuristic, by Paul Eggert, to limit the cost to O(N**1.5 log N)
  27.    at the price of producing suboptimal output for large inputs with
  28.    many differences.
  29.  
  30.    The basic algorithm was independently discovered as described in:
  31.    "Algorithms for Approximate String Matching", E. Ukkonen,
  32.    Information and Control Vol. 64, 1985, pp. 100-118.  */
  33.  
  34. #include "diff.h"
  35. #include <limits.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38.  
  39. static int *xvec, *yvec;                /* Vectors being compared. */
  40. static int *fdiag;                            /* Vector, indexed by diagonal, containing
  41.                                                                      the X coordinate of the point furthest
  42.                                                                      along the given diagonal in the forward
  43.                                                                      search of the edit matrix. */
  44. static int *bdiag;                            /* Vector, indexed by diagonal, containing
  45.                                                                      the X coordinate of the point furthest
  46.                                                                      along the given diagonal in the backward
  47.                                                                      search of the edit matrix. */
  48.  
  49. static int too_expensive;                /* Edit scripts longer than this are too
  50.                                                                      expensive to compute.  */
  51.  
  52. #define SNAKE_LIMIT 20    /* Snakes bigger than this are considered `big'.  */
  53.  
  54. struct partition
  55. {
  56.   int xmid, ymid;    /* Midpoints of this partition.  */
  57.   int lo_minimal;    /* Nonzero if low half will be analyzed minimally.  */
  58.   int hi_minimal;    /* Likewise for high half.  */
  59. };
  60.  
  61. static struct change     *add_change (int line0, int line1, int deleted, int inserted, struct change *old);
  62. static struct change     *build_script (struct file_data *file0Ptr, struct file_data *file1Ptr);
  63. static void                         compareseq (struct file_data *file0Ptr, struct file_data *file1Ptr,
  64.                                                                         int xoff, int xlim, int yoff, int ylim, int minimal);
  65. static int                            diag (int xoff, int xlim, int yoff, int ylim, int minimal, struct partition *part);
  66. static void                         discard_confusing_lines (register struct file_data *file0Ptr,
  67.                                                                                                  register struct file_data *file1Ptr);
  68. static void                         shift_boundaries (struct file_data *file0Ptr, struct file_data *file1Ptr);
  69.  
  70. /* Cons an additional entry onto the front of an edit script OLD.
  71.      LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  72.      DELETED is the number of lines deleted here from file 0.
  73.      INSERTED is the number of lines inserted here in file 1.
  74.  
  75.      If DELETED is 0 then LINE0 is the number of the line before
  76.      which the insertion was done; vice versa for INSERTED and LINE1.  */
  77.  
  78. static struct change *
  79. add_change (int line0, int line1, int deleted, int inserted, struct change *old)
  80. {
  81.     struct change *new = (struct change *) xmalloc (sizeof (struct change));
  82.  
  83.     new->line0 = line0;
  84.     new->line1 = line1;
  85.     new->inserted = inserted;
  86.     new->deleted = deleted;
  87.     new->link = old;
  88.     return new;
  89. }
  90.  
  91. /* Scan the tables of which lines are inserted and deleted,
  92.      producing an edit script in forward order.  */
  93.  
  94. static struct change *
  95. build_script (struct file_data *file0Ptr, struct file_data *file1Ptr)
  96. {
  97.     struct change *script = 0;
  98.     char *changed0 = file0Ptr->changed_flag;
  99.     char *changed1 = file1Ptr->changed_flag;
  100.     int len0 = file0Ptr->buffered_lines;
  101.     int len1 = file1Ptr->buffered_lines;
  102.     int i0 = len0, i1 = len1;
  103.  
  104.     /* Note that changedN[-1] does exist, and contains 0.  */
  105.  
  106.     while (i0 >= 0 || i1 >= 0)
  107.         {
  108.             if (changed0[i0 - 1] || changed1[i1 - 1])
  109.                 {
  110.                     int line0 = i0, line1 = i1;
  111.  
  112.                     /* Find # lines changed here in each file.    */
  113.                     while (changed0[i0 - 1]) --i0;
  114.                     while (changed1[i1 - 1]) --i1;
  115.  
  116.                     /* Record this change.    */
  117.                     script = add_change (i0, i1, line0 - i0, line1 - i1, script);
  118.                 }
  119.  
  120.             /* We have reached lines in the two files that match each other.    */
  121.             i0--, i1--;
  122.         }
  123.  
  124.     return script;
  125. }
  126.  
  127. /* Compare in detail contiguous subsequences of the two files
  128.    which are known, as a whole, to match each other.
  129.  
  130.    The results are recorded in the vectors files[N].changed_flag, by
  131.    storing a 1 in the element for each line that is an insertion or deletion.
  132.  
  133.    The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
  134.  
  135.    Note that XLIM, YLIM are exclusive bounds.
  136.    All line numbers are origin-0 and discarded lines are not counted.
  137.  
  138.    If MINIMAL is nonzero, find a minimal difference no matter how
  139.    expensive it is.  */
  140.  
  141. static void
  142. compareseq (struct file_data *file0Ptr, struct file_data *file1Ptr,
  143.                         int xoff, int xlim, int yoff, int ylim, int minimal)
  144. {
  145.   int * const xv = xvec; /* Help the compiler.  */
  146.   int * const yv = yvec;
  147.  
  148.     /* Slide down the bottom initial diagonal. */
  149.     while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff])
  150.         ++xoff, ++yoff;
  151.     /* Slide up the top initial diagonal. */
  152.     while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1])
  153.         --xlim, --ylim;
  154.     
  155.     /* Handle simple cases. */
  156.     if (xoff == xlim)
  157.         while (yoff < ylim)
  158.             file1Ptr->changed_flag[file1Ptr->realindexes[yoff++]] = 1;
  159.     else if (yoff == ylim)
  160.         while (xoff < xlim)
  161.             file0Ptr->changed_flag[file0Ptr->realindexes[xoff++]] = 1;
  162.     else
  163.         {
  164.             int c;
  165.             struct partition part;
  166.  
  167.             /* Find a point of correspondence in the middle of the files.  */
  168.  
  169.             c = diag (xoff, xlim, yoff, ylim, minimal, &part);
  170.  
  171.             if (c == 1)
  172.                 {
  173.                     /* This should be impossible, because it implies that
  174.                          one of the two subsequences is empty,
  175.                          and that case was handled above without calling `diag'.
  176.                          Let's verify that this is true.  */
  177.                     abort ();
  178. #if 0
  179.                     /* The two subsequences differ by a single insert or delete;
  180.                          record it and we are done.  */
  181.                     if (part.xmid - part.ymid < xoff - yoff)
  182.                         files[1].changed_flag[files[1].realindexes[part.ymid - 1]] = 1;
  183.                     else
  184.                         files[0].changed_flag[files[0].realindexes[part.xmid]] = 1;
  185. #endif
  186.                 }
  187.       else
  188.                 {
  189.                     /* Use the partitions to split this problem into subproblems.  */
  190.                     compareseq (file0Ptr, file1Ptr, xoff, part.xmid, yoff, part.ymid, part.lo_minimal);
  191.                     compareseq (file0Ptr, file1Ptr, part.xmid, xlim, part.ymid, ylim, part.hi_minimal);
  192.                 }
  193.         }
  194. }
  195.  
  196. /* Find the midpoint of the shortest edit script for a specified
  197.    portion of the two files.
  198.  
  199.    Scan from the beginnings of the files, and simultaneously from the ends,
  200.    doing a breadth-first search through the space of edit-sequence.
  201.    When the two searches meet, we have found the midpoint of the shortest
  202.    edit sequence.
  203.  
  204.    If MINIMAL is nonzero, find the minimal edit script regardless
  205.    of expense.  Otherwise, if the search is too expensive, use
  206.    heuristics to stop the search and report a suboptimal answer.
  207.  
  208.    Set PART->(XMID,YMID) to the midpoint (XMID,YMID).  The diagonal number
  209.    XMID - YMID equals the number of inserted lines minus the number
  210.    of deleted lines (counting only lines before the midpoint).
  211.    Return the approximate edit cost; this is the total number of
  212.    lines inserted or deleted (counting only lines before the midpoint),
  213.    unless a heuristic is used to terminate the search prematurely.
  214.  
  215.    Set PART->LEFT_MINIMAL to nonzero iff the minimal edit script for the
  216.    left half of the partition is known; similarly for PART->RIGHT_MINIMAL.
  217.  
  218.    This function assumes that the first lines of the specified portions
  219.    of the two files do not match, and likewise that the last lines do not
  220.    match.  The caller must trim matching lines from the beginning and end
  221.    of the portions it is going to specify.
  222.  
  223.    If we return the "wrong" partitions,
  224.    the worst this can do is cause suboptimal diff output.
  225.    It cannot cause incorrect diff output.  */
  226.  
  227. static int
  228. diag (int xoff, int xlim, int yoff, int ylim, int minimal, struct partition *part)
  229. {
  230.     int *const fd = fdiag;                /* Give the compiler a chance. */
  231.     int *const bd = bdiag;                /* Additional help for the compiler. */
  232.     int *const xv = xvec;                 /* Still more help for the compiler. */
  233.     int *const yv = yvec;                 /* And more and more . . . */
  234.     const int dmin = xoff - ylim; /* Minimum valid diagonal. */
  235.     const int dmax = xlim - yoff; /* Maximum valid diagonal. */
  236.     const int fmid = xoff - yoff; /* Center diagonal of top-down search. */
  237.     const int bmid = xlim - ylim; /* Center diagonal of bottom-up search. */
  238.     int fmin = fmid, fmax = fmid; /* Limits of top-down search. */
  239.     int bmin = bmid, bmax = bmid; /* Limits of bottom-up search. */
  240.     int c;                                                /* Cost. */
  241.     int odd = (fmid - bmid) & 1;    /* True if southeast corner is on an odd
  242.                                                                      diagonal with respect to the northwest. */
  243.  
  244.     fd[fmid] = xoff;
  245.     bd[bmid] = xlim;
  246.  
  247.     for (c = 1;; ++c)
  248.         {
  249.             int d;                                        /* Active diagonal. */
  250.             int big_snake = 0;
  251.  
  252.             /* Extend the top-down search by an edit step in each diagonal. */
  253.             fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin;
  254.             fmax < dmax ? fd[++fmax + 1] = -1 : --fmax;
  255.             for (d = fmax; d >= fmin; d -= 2)
  256.                 {
  257.                     int x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1];
  258.  
  259.                     if (tlo >= thi)
  260.                         x = tlo + 1;
  261.                     else
  262.                         x = thi;
  263.                     oldx = x;
  264.                     y = x - d;
  265.                     while (x < xlim && y < ylim && xv[x] == yv[y])
  266.                         ++x, ++y;
  267.                     if (x - oldx > SNAKE_LIMIT)
  268.                         big_snake = 1;
  269.                     fd[d] = x;
  270.                     if (odd && bmin <= d && d <= bmax && bd[d] <= x)
  271.                         {
  272.                             part->xmid = x;
  273.                             part->ymid = y;
  274.                             part->lo_minimal = part->hi_minimal = 1;
  275.                             return 2 * c - 1;
  276.                         }
  277.                 }
  278.  
  279.             /* Similar extend the bottom-up search. */
  280.             bmin > dmin ? bd[--bmin - 1] = INT_MAX : ++bmin;
  281.             bmax < dmax ? bd[++bmax + 1] = INT_MAX : --bmax;
  282.             for (d = bmax; d >= bmin; d -= 2)
  283.                 {
  284.                     int x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
  285.  
  286.                     if (tlo < thi)
  287.                         x = tlo;
  288.                     else
  289.                         x = thi - 1;
  290.                     oldx = x;
  291.                     y = x - d;
  292.                     while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
  293.                         --x, --y;
  294.                     if (oldx - x > SNAKE_LIMIT)
  295.                         big_snake = 1;
  296.                     bd[d] = x;
  297.                     if (!odd && fmin <= d && d <= fmax && x <= fd[d])
  298.                         {
  299.                             part->xmid = x;
  300.                             part->ymid = y;
  301.                             part->lo_minimal = part->hi_minimal = 1;
  302.                             return 2 * c;
  303.                         }
  304.                 }
  305.  
  306.       if (minimal)
  307.                 continue;
  308.  
  309.             /* Heuristic: check occasionally for a diagonal that has made
  310.                  lots of progress compared with the edit distance.
  311.                  If we have any such, find the one that has made the most
  312.                  progress and return it as if it had succeeded.
  313.  
  314.                  With this heuristic, for files with a constant small density
  315.                  of changes, the algorithm is linear in the file size.    */
  316.  
  317.             if (c > 200 && big_snake)
  318.                 {
  319.                     int best;
  320.  
  321.                     best = 0;
  322.                     for (d = fmax; d >= fmin; d -= 2)
  323.                         {
  324.                             int dd = d - fmid;
  325.                             int x = fd[d];
  326.                             int y = x - d;
  327.                             int v = (x - xoff) * 2 - dd;
  328.                             if (v > 12 * (c + (dd < 0 ? -dd : dd)))
  329.                             if ((fd[d] - xoff)*2 - dd > 12 * (c + (dd > 0 ? dd : -dd)))
  330.                                 {
  331.                                     if (v > best
  332.                                             && xoff + SNAKE_LIMIT <= x && x < xlim
  333.                                             && yoff + SNAKE_LIMIT <= y && y < ylim)
  334.                                         {
  335.                                             /* We have a good enough best diagonal;
  336.                                                  now insist that it end with a significant snake.  */
  337.                                             int k;
  338.  
  339.                                             for (k = 1; xv[x - k] == yv[y - k]; k++)
  340.                                                 if (k == SNAKE_LIMIT)
  341.                                                     {
  342.                                                         best = v;
  343.                                                         part->xmid = x;
  344.                                                         part->ymid = y;
  345.                                                         break;
  346.                                                     }
  347.                                         }
  348.                                 }
  349.                         }
  350.                     if (best > 0)
  351.                         {
  352.                             part->lo_minimal = 1;
  353.                             part->hi_minimal = 0;
  354.                             return 2 * c - 1;
  355.                         }
  356.  
  357.                     best = 0;
  358.                     for (d = bmax; d >= bmin; d -= 2)
  359.                         {
  360.                             int dd = d - bmid;
  361.                             int x = bd[d];
  362.                             int y = x - d;
  363.                             int v = (xlim - x) * 2 + dd;
  364.                             if (v > 12 * (c + (dd < 0 ? -dd : dd)))
  365.                                 {
  366.                                     if (v > best
  367.                                             && xoff < x && x <= xlim - SNAKE_LIMIT
  368.                                             && yoff < y && y <= ylim - SNAKE_LIMIT)
  369.                                         {
  370.                                             /* We have a good enough best diagonal;
  371.                                                  now insist that it end with a significant snake.  */
  372.                                             int k;
  373.  
  374.                                             for (k = 0; xv[x + k] == yv[y + k]; k++)
  375.                                                 if (k == SNAKE_LIMIT - 1)
  376.                                                     {
  377.                                                         best = v;
  378.                                                         part->xmid = x;
  379.                                                         part->ymid = y;
  380.                                                         break;
  381.                                                     }
  382.                                         }
  383.                                 }
  384.                         }
  385.                     if (best > 0)
  386.                         {
  387.                             part->lo_minimal = 0;
  388.                             part->hi_minimal = 1;
  389.                             return 2 * c - 1;
  390.                         }
  391.                 }
  392.  
  393.             /* Heuristic: if we've gone well beyond the call of duty,
  394.                  give up and report halfway between our best results so far.  */
  395.             if (c >= too_expensive)
  396.                 {
  397.                     int fxybest, fxbest;
  398.                     int bxybest, bxbest;
  399.  
  400.                     fxbest = bxbest = 0;  /* Pacify `gcc -Wall'.  */
  401.  
  402.                     /* Find forward diagonal that maximizes X + Y.  */
  403.                     fxybest = -1;
  404.                     for (d = fmax; d >= fmin; d -= 2)
  405.                         {
  406.                             int x = (fd[d] < xlim ? fd[d] : xlim);
  407.                             int y = x - d;
  408.                             if (ylim < y)
  409.                                 x = ylim + d, y = ylim;
  410.                             if (fxybest < x + y)
  411.                                 {
  412.                                     fxybest = x + y;
  413.                                     fxbest = x;
  414.                                 }
  415.                         }
  416.  
  417.                     /* Find backward diagonal that minimizes X + Y.  */
  418.                     bxybest = INT_MAX;
  419.                     for (d = bmax; d >= bmin; d -= 2)
  420.                         {
  421.                             int x = (xoff > bd[d] ? xoff : bd[d]);
  422.                             int y = x - d;
  423.                             if (y < yoff)
  424.                                 x = yoff + d, y = yoff;
  425.                             if (x + y < bxybest)
  426.                                 {
  427.                                     bxybest = x + y;
  428.                                     bxbest = x;
  429.                                 }
  430.                         }
  431.  
  432.                     /* Use the better of the two diagonals.  */
  433.                     if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
  434.                         {
  435.                             part->xmid = fxbest;
  436.                             part->ymid = fxybest - fxbest;
  437.                             part->lo_minimal = 1;
  438.                             part->hi_minimal = 0;
  439.                         }
  440.                     else
  441.                         {
  442.                             part->xmid = bxbest;
  443.                             part->ymid = bxybest - bxbest;
  444.                             part->lo_minimal = 0;
  445.                             part->hi_minimal = 1;
  446.                         }
  447.                     return 2 * c - 1;
  448.                 }
  449.         }
  450. }
  451.  
  452. /* Report the differences of two files. */
  453.  
  454. void
  455. diff_2_files (register struct file_data *file0Ptr, register struct file_data *file1Ptr)
  456. {
  457.     int                      diags;
  458.   int                         i;
  459.     struct change *nextScriptPtr;
  460.     struct change *scriptPtr;
  461.  
  462.     read_files (file0Ptr, file1Ptr);
  463.  
  464.     scriptPtr = file1Ptr->scriptPtr;
  465.     while (scriptPtr != NULL) {
  466.         nextScriptPtr = scriptPtr->link;
  467.         free (scriptPtr);
  468.         scriptPtr = nextScriptPtr;
  469.     }
  470.     file1Ptr->scriptPtr = NULL;
  471.  
  472.     if (file0Ptr->buffer != NULL && file1Ptr->buffer != NULL) {
  473.     /*
  474.      * Allocate vectors for the results of comparison:
  475.      * a flag for each line of each file, saying whether that line
  476.      * is an insertion or deletion.
  477.      * Allocate an extra element, always zero, at each end of each vector.
  478.      */
  479.         if (file0Ptr->changed_flag == NULL) {
  480.             file0Ptr->changed_flag = (char *) xmalloc (file0Ptr->buffered_lines + 2);
  481.             file0Ptr->changed_flag++;
  482.         }
  483.  
  484.         if (file1Ptr->changed_flag == NULL) {
  485.             file1Ptr->changed_flag = (char *) xmalloc (file1Ptr->buffered_lines + 2);
  486.             file1Ptr->changed_flag++;
  487.         }
  488.  
  489.         bzero (file0Ptr->changed_flag - 1, file0Ptr->buffered_lines + 2);
  490.         bzero (file1Ptr->changed_flag - 1, file1Ptr->buffered_lines + 2);
  491.  /*
  492.     * Some lines are obviously insertions or deletions
  493.     * because they don't match anything.  Detect them now,
  494.     * and avoid even thinking about them in the main comparison algorithm.
  495.     */
  496.         discard_confusing_lines (file0Ptr, file1Ptr);
  497.  /*
  498.     * Now do the main comparison algorithm, considering just the
  499.     * undiscarded lines.
  500.     */
  501.         xvec = file0Ptr->undiscarded;
  502.         yvec = file1Ptr->undiscarded;
  503.         diags = file0Ptr->nondiscarded_lines + file1Ptr->nondiscarded_lines + 3;
  504.         fdiag = (int *) xmalloc (diags * (2 * sizeof (int)));
  505.         bdiag = fdiag + diags;
  506.         fdiag += file1Ptr->nondiscarded_lines + 1;
  507.         bdiag += file1Ptr->nondiscarded_lines + 1;
  508.  
  509.     /* Set TOO_EXPENSIVE to be approximate square root of input size,
  510.              bounded below by 256.  */
  511.         too_expensive = 1;
  512.         for (i = file0Ptr->nondiscarded_lines + file1Ptr->nondiscarded_lines; i != 0; i >>= 2)
  513.             too_expensive <<= 1;
  514.         too_expensive = (256 > too_expensive ? 256 : too_expensive);
  515.  
  516.             compareseq (file0Ptr, file1Ptr, 0, file0Ptr->nondiscarded_lines, 0, file1Ptr->nondiscarded_lines, /*minimal is*/ 1);
  517.  
  518.         free (fdiag - (file1Ptr->nondiscarded_lines + 1));
  519.  /*
  520.     * Modify the results slightly to make them prettier
  521.     * in cases where that can validly be done.
  522.     */
  523.         shift_boundaries (file0Ptr, file1Ptr);
  524.  /*
  525.     * Get the results of comparison in the form of a chain
  526.     * of `struct change's -- an edit script.
  527.     */
  528.         file1Ptr->scriptPtr = build_script (file0Ptr, file1Ptr);
  529.     }
  530. }
  531.  
  532. /* Discard lines from one file that have no matches in the other file.
  533.  
  534.      A line which is discarded will not be considered by the actual
  535.      comparison algorithm; it will be as if that line were not in the file.
  536.      The file's `realindexes' table maps virtual line numbers
  537.      (which don't count the discarded lines) into real line numbers;
  538.      this is how the actual comparison algorithm produces results
  539.      that are comprehensible when the discarded lines are counted.
  540.  
  541.      When we discard a line, we also mark it as a deletion or insertion
  542.      so that it will be printed in the output.    */
  543.  
  544. void
  545. discard_confusing_lines (register struct file_data *file0Ptr, register struct file_data *file1Ptr)
  546. {
  547.     char                                 *discarded[2];
  548.     int                                  *equiv_count[2];
  549.     struct file_data         *filePtr;
  550.     unsigned                            int f, i;
  551.  
  552.     /* Allocate our results.    */
  553.     filePtr = file0Ptr;
  554.     for (f = 0; f < 2; f++) {
  555.         if (filePtr->undiscarded == NULL)
  556.             filePtr->undiscarded = (int *) xmalloc (filePtr->buffered_lines * sizeof (int));
  557.         if (filePtr->realindexes == NULL)
  558.          filePtr->realindexes = (int *) xmalloc (filePtr->buffered_lines * sizeof (int));
  559.         filePtr = file1Ptr;
  560.     }
  561.  
  562.     /* Set up equiv_count[F][I] as the number of lines in file F
  563.          that fall in equivalence class I.    */
  564.  
  565.     equiv_count[0] = (int *) xmalloc (file0Ptr->equiv_max * sizeof (int));
  566.     bzero (equiv_count[0], file0Ptr->equiv_max * sizeof (int));
  567.     equiv_count[1] = (int *) xmalloc (file1Ptr->equiv_max * sizeof (int));
  568.     bzero (equiv_count[1], file1Ptr->equiv_max * sizeof (int));
  569.  
  570.     for (i = 0; i < file0Ptr->buffered_lines; ++i)
  571.         ++equiv_count[0][file0Ptr->equivs[i]];
  572.     for (i = 0; i < file1Ptr->buffered_lines; ++i)
  573.         ++equiv_count[1][file1Ptr->equivs[i]];
  574.  
  575.     /* Set up tables of which lines are going to be discarded.    */
  576.  
  577.     discarded[0] = (char *) xmalloc (file0Ptr->buffered_lines);
  578.     discarded[1] = (char *) xmalloc (file1Ptr->buffered_lines);
  579.     bzero (discarded[0], file0Ptr->buffered_lines);
  580.     bzero (discarded[1], file1Ptr->buffered_lines);
  581.  
  582.     /* Mark to be discarded each line that matches no line of the other file.
  583.          If a line matches many lines, mark it as provisionally discardable.    */
  584.  
  585.     filePtr = file0Ptr;
  586.     for (f = 0; f < 2; f++)
  587.         {
  588.             unsigned int end = filePtr->buffered_lines;
  589.             char *discards = discarded[f];
  590.             int *counts = equiv_count[1 - f];
  591.             int *equivs = filePtr->equivs;
  592.             unsigned int many = 5;
  593.             unsigned int tem = end / 64;
  594.  
  595.             /* Multiply MANY by approximate square root of number of lines.
  596.                  That is the threshold for provisionally discardable lines.  */
  597.             while ((tem = tem >> 2) > 0)
  598.                 many *= 2;
  599.  
  600.             for (i = 0; i < end; i++)
  601.                 {
  602.                     unsigned int nmatch;
  603.                     if (equivs[i] == 0)
  604.                         continue;
  605.                     nmatch = counts[equivs[i]];
  606.                     if (nmatch == 0)
  607.                         discards[i] = 1;
  608.                     else if (nmatch > many)
  609.                         discards[i] = 2;
  610.                 }
  611.             filePtr = file1Ptr;
  612.         }
  613.  
  614.     /* Don't really discard the provisional lines except when they occur
  615.          in a run of discardables, with nonprovisionals at the beginning
  616.          and end.  */
  617.  
  618.     filePtr = file0Ptr;
  619.     for (f = 0; f < 2; f++)
  620.         {
  621.             unsigned int end = filePtr->buffered_lines;
  622.             register char *discards = discarded[f];
  623.  
  624.             for (i = 0; i < end; i++)
  625.                 {
  626.                     /* Cancel provisional discards not in middle of run of discards.    */
  627.                     if (discards[i] == 2)
  628.                         discards[i] = 0;
  629.                     else if (discards[i] != 0)
  630.                         {
  631.                             /* We have found a nonprovisional discard.    */
  632.                             register unsigned int j;
  633.                             unsigned int length;
  634.                             unsigned int provisional = 0;
  635.  
  636.                             /* Find end of this run of discardable lines.
  637.                                  Count how many are provisionally discardable.    */
  638.                             for (j = i; j < end; j++)
  639.                                 {
  640.                                     if (discards[j] == 0)
  641.                                         break;
  642.                                     if (discards[j] == 2)
  643.                                         ++provisional;
  644.                                 }
  645.  
  646.                             /* Cancel provisional discards at end, and shrink the run.    */
  647.                             while (j > i && discards[j - 1] == 2)
  648.                                 discards[--j] = 0, --provisional;
  649.  
  650.                             /* Now we have the length of a run of discardable lines
  651.                                  whose first and last are not provisional.    */
  652.                             length = j - i;
  653.  
  654.                             /* If 1/4 of the lines in the run are provisional,
  655.                                  cancel discarding of all provisional lines in the run.  */
  656.                             if (provisional * 4 > length)
  657.                                 {
  658.                                     while (j > i)
  659.                                         if (discards[--j] == 2)
  660.                                             discards[j] = 0;
  661.                                 }
  662.                             else
  663.                                 {
  664.                                     register unsigned int consec;
  665.                                     unsigned int minimum = 1;
  666.                                     unsigned int tem = length / 4;
  667.  
  668.                                     /* MINIMUM is approximate square root of LENGTH/4.
  669.                                          A subrun of two or more provisionals can stand
  670.                                          when LENGTH is at least 16.
  671.                                          A subrun of 4 or more can stand when LENGTH >= 64.  */
  672.                                     while ((tem = tem >> 2) > 0)
  673.                                         minimum *= 2;
  674.                                     minimum++;
  675.  
  676.                                     /* Cancel any subrun of MINIMUM or more provisionals
  677.                                          within the larger run.  */
  678.                                     for (j = 0, consec = 0; j < length; j++)
  679.                                         if (discards[i + j] != 2)
  680.                                             consec = 0;
  681.                                         else if (minimum == ++consec)
  682.                                             /* Back up to start of subrun, to cancel it all.    */
  683.                                             j -= consec;
  684.                                         else if (minimum < consec)
  685.                                             discards[i + j] = 0;
  686.  
  687.                                     /* Scan from beginning of run
  688.                                          until we find 3 or more nonprovisionals in a row
  689.                                          or until the first nonprovisional at least 8 lines in.
  690.                                          Until that point, cancel any provisionals.  */
  691.                                     for (j = 0, consec = 0; j < length; j++)
  692.                                         {
  693.                                             if (j >= 8 && discards[i + j] == 1)
  694.                                                 break;
  695.                                             if (discards[i + j] == 2)
  696.                                                 consec = 0, discards[i + j] = 0;
  697.                                             else if (discards[i + j] == 0)
  698.                                                 consec = 0;
  699.                                             else
  700.                                                 consec++;
  701.                                             if (consec == 3)
  702.                                                 break;
  703.                                         }
  704.  
  705.                                     /* I advances to the last line of the run.    */
  706.                                     i += length - 1;
  707.  
  708.                                     /* Same thing, from end.    */
  709.                                     for (j = 0, consec = 0; j < length; j++)
  710.                                         {
  711.                                             if (j >= 8 && discards[i - j] == 1)
  712.                                                 break;
  713.                                             if (discards[i - j] == 2)
  714.                                                 consec = 0, discards[i - j] = 0;
  715.                                             else if (discards[i - j] == 0)
  716.                                                 consec = 0;
  717.                                             else
  718.                                                 consec++;
  719.                                             if (consec == 3)
  720.                                                 break;
  721.                                         }
  722.                                 }
  723.                         }
  724.                 }
  725.             filePtr = file1Ptr;
  726.         }
  727.  
  728.     /* Actually discard the lines. */
  729.     filePtr = file0Ptr;
  730.     for (f = 0; f < 2; f++)
  731.         {
  732.             char *discards = discarded[f];
  733.             unsigned int end = filePtr->buffered_lines;
  734.             unsigned int j = 0;
  735.             for (i = 0; i < end; ++i)
  736.                 if (discards[i] == 0)
  737.                     {
  738.                         filePtr->undiscarded[j] = filePtr->equivs[i];
  739.                         filePtr->realindexes[j++] = i;
  740.                     }
  741.                 else
  742.                     filePtr->changed_flag[i] = 1;
  743.             filePtr->nondiscarded_lines = j;
  744.             filePtr = file1Ptr;
  745.         }
  746.  
  747.     free (discarded[1]);
  748.     free (discarded[0]);
  749.     free (equiv_count[1]);
  750.     free (equiv_count[0]);
  751. }
  752.  
  753. void freeFile (register struct file_data *filePtr)
  754. {
  755.     struct change *nextScriptPtr;
  756.     struct change *scriptPtr;
  757.  
  758.   if (filePtr->desc != NULL) {
  759.         fclose (filePtr->desc);
  760.         filePtr->desc = NULL;
  761.     }
  762.     
  763.     if (filePtr->undiscarded != NULL) {
  764.         free (filePtr->undiscarded);
  765.         filePtr->undiscarded = NULL;
  766.     }
  767.  
  768.     if (filePtr->realindexes != NULL) {
  769.         free (filePtr->realindexes);
  770.         filePtr->realindexes = NULL;
  771.     }
  772.  
  773.     if (filePtr->changed_flag != NULL) {
  774.         free (--filePtr->changed_flag);
  775.         filePtr->changed_flag = NULL;
  776.     }
  777.  
  778.     if (filePtr->equivs != NULL) {
  779.         free (filePtr->equivs);
  780.         filePtr->equivs = NULL;
  781.     }
  782.  
  783.     if (filePtr->buffer != NULL) {
  784.         free (filePtr->buffer);
  785.         filePtr->buffer = NULL;
  786.     }
  787.  
  788.     if (filePtr->linbuf != NULL) {
  789.         free (filePtr->linbuf);
  790.         filePtr->linbuf = NULL;
  791.     }
  792.  
  793.     scriptPtr = filePtr->scriptPtr;
  794.     while (scriptPtr != NULL) {
  795.         nextScriptPtr = scriptPtr->link;
  796.         free (scriptPtr);
  797.         scriptPtr = nextScriptPtr;
  798.     }
  799.     filePtr->scriptPtr = NULL;
  800. }
  801.  
  802. /* Adjust inserts/deletes of blank lines to join changes
  803.      as much as possible.
  804.  
  805.      We do something when a run of changed lines include a blank
  806.      line at one end and have an excluded blank line at the other.
  807.      We are free to choose which blank line is included.
  808.      `compareseq' always chooses the one at the beginning,
  809.      but usually it is cleaner to consider the following blank line
  810.      to be the "change".    The only exception is if the preceding blank line
  811.      would join this change to other changes.  */
  812.  
  813. static void
  814. shift_boundaries (struct file_data *file0Ptr, struct file_data *file1Ptr)
  815. {
  816.     int                                                     f;
  817.     register struct file_data         *filePtr;
  818.  
  819.     filePtr = file0Ptr;
  820.     for (f = 0; f < 2; f++)
  821.         {
  822.             char *changed;
  823.             char *other_changed;
  824.       int const *equivs;
  825.             int i = 0;
  826.             int j = 0;
  827.             int i_end = filePtr->buffered_lines;
  828.  
  829.             changed = filePtr->changed_flag;
  830.             other_changed = file0Ptr->changed_flag;
  831.             equivs = filePtr->equivs;
  832.             if (filePtr == file0Ptr)
  833.                 other_changed = file1Ptr->changed_flag;
  834.             while (1)
  835.                 {
  836.                     int runlength, start, corresponding;
  837.  
  838.                     /* Scan forwards to find beginning of another run of changes.
  839.                          Also keep track of the corresponding point in the other file.  */
  840.  
  841.                     while (i < i_end && changed[i] == 0)
  842.                         {
  843.                             while (other_changed[j++])
  844.                                 continue;
  845.                             i++;
  846.                         }
  847.  
  848.                     if (i == i_end)
  849.                         break;
  850.  
  851.                     start = i;
  852.  
  853.                     /* Find the end of this run of changes.  */
  854.  
  855.                     while (changed[++i])
  856.                         continue;
  857.                     while (other_changed[j])
  858.                         j++;
  859.  
  860.                     do
  861.                         {
  862.                             /* Record the length of this run of changes, so that
  863.                                  we can later determine whether the run has grown.  */
  864.                             runlength = i - start;
  865.  
  866.                             /* Move the changed region back, so long as the
  867.                                  previous unchanged line matches the last changed one.
  868.                                  This merges with previous changed regions.  */
  869.  
  870.                             while (start && equivs[start - 1] == equivs[i - 1])
  871.                                 {
  872.                                     changed[--start] = 1;
  873.                                     changed[--i] = 0;
  874.                                     while (changed[start - 1])
  875.                                         start--;
  876.                                     while (other_changed[--j])
  877.                                         continue;
  878.                                 }
  879.  
  880.                             /* Set CORRESPONDING to the end of the changed run, at the last
  881.                                  point where it corresponds to a changed run in the other file.
  882.                                  CORRESPONDING == I_END means no such point has been found.  */
  883.                             corresponding = other_changed[j - 1] ? i : i_end;
  884.  
  885.                             /* Move the changed region forward, so long as the
  886.                                  first changed line matches the following unchanged one.
  887.                                  This merges with following changed regions.
  888.                                  Do this second, so that if there are no merges,
  889.                                  the changed region is moved forward as far as possible.  */
  890.  
  891.                             while (i != i_end && equivs[start] == equivs[i])
  892.                                 {
  893.                                     changed[start++] = 0;
  894.                                     changed[i++] = 1;
  895.                                     while (changed[i])
  896.                                         i++;
  897.                                     while (other_changed[++j])
  898.                                         corresponding = i;
  899.                                 }
  900.                         }
  901.                     while (runlength != i - start);
  902.  
  903.                     /* If possible, move the fully-merged run of changes
  904.                          back to a corresponding run in the other file.  */
  905.  
  906.                     while (corresponding < i)
  907.                         {
  908.                             changed[--start] = 1;
  909.                             changed[--i] = 0;
  910.                             while (other_changed[--j])
  911.                                 continue;
  912.                         }
  913.  
  914.                 }
  915.             filePtr = file1Ptr;
  916.         }
  917. }
  918.  
  919.